package me.zhengjie.modules.system.service.mapstruct.convertor.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.spire.ms.System.Collections.ArrayList;
import lombok.RequiredArgsConstructor;
import me.zhengjie.config.MappingProperties;
import me.zhengjie.modules.system.domain.Document;
import me.zhengjie.modules.system.repository.DocumentRepository;
import me.zhengjie.modules.system.service.mapstruct.convertor.SerialNoConvertor;
import me.zhengjie.util.FileTypeEnum;
import me.zhengjie.util.JacksonUtil;
import me.zhengjie.utils.StringUtils;
import org.mapstruct.Named;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;

/**
 * Mapping通用转换
 *
 * @author guoyuan
 * @version 1.0.0
 * @since 1.0.0
 */
@Component
@RequiredArgsConstructor
@Named("documentConvertor")
public class DocumentConvertor implements SerialNoConvertor {

    private static final String SPLIT = "-";
    private final DocumentRepository documentRepository;
    private final MappingProperties properties;

    /**
     * 对象转json字符串
     *
     * @param obj /
     * @return /
     */
    @Named("toJsonString")
    public String toJsonString(Object obj) {
        if (Objects.isNull(obj)) {
            return null;
        }
        return JacksonUtil.toJsonString(obj);
    }

    @Named("docNum2String")
    public String docNum2String(Integer docNum, Timestamp createTime, String docType) {
        final Document first = documentRepository.findFirstByDocTypeOrderByDocNumDesc(docType);
        final List<String> docNumPrefix = properties.getDocNumPrefix();
        final Integer docNumZeroLen = properties.getDocNumZeroLen();
        final String docNumTimePattern = properties.getDocNumTimePattern();
        final String part1 = docNumPrefix.get(Integer.parseInt(docType));
        final String part2 = new SimpleDateFormat(docNumTimePattern).format(createTime);
        final String prefix = part1 + SPLIT + part2 + SPLIT;
        String strDocNum;
        if (first != null) {
            strDocNum = buildSerialNo(docNum, prefix, docNumZeroLen, first.getDocNum());
        } else {
            strDocNum = prefix + docNum;
        }
        return strDocNum;
    }

    @Named("docNum2Integer")
    public Integer docNum2Integer(String docNum) {
        if (StringUtils.isBlank(docNum)) {
            return null;
        }
        final int i = docNum.lastIndexOf(SPLIT) + 1;
        final String strDocNum = docNum.substring(i);
        return Integer.parseInt(strDocNum);
    }

    /**
     * 字符串转List对象
     *
     * @param str /
     * @return /
     */
    @Named("toStrList")
    public <T> List<T> toStrList(String str, Class<T> elementClasses) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        List<T> ts = null;
        try {
            ts = JacksonUtil.jsonToObjByTypeRf(str, ArrayList.class, elementClasses);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return ts;
    }

    @Named("fileType2Desc")
    public String fileType2Desc(String fileType) throws Exception {
        return FileTypeEnum.getDesc(fileType);
    }

    @Named("desc2FileType")
    public String desc2FileType(String desc) throws Exception {
        return FileTypeEnum.getType(desc);
    }
}
